<?php
class ModelCommunityCron extends Model {
    public function updateCrons($data){
        $query=$this->db->query("SELECT * FROM " . DB_PREFIX . "community_crons");

        $crons=array();

        foreach($query->rows as $cron){
            $crons[$cron['cron_id']]=$cron;
        }

        $this->db->query("DELETE FROM " . DB_PREFIX . "community_crons");

        foreach($data['cron'] as $cron){
            if(!empty($cron['cron_id'])){
                $this->db->query("INSERT INTO " . DB_PREFIX . "community_crons SET cron_id='" . (int)$crons[$cron['cron_id']]['cron_id'] . "', available='" . (int)$cron['available'] . "', name='" . $this->db->escape(trim($cron['name'])) . "', file_name='" . $this->db->escape(trim($cron['file_name'])) . "', last_run='" . $crons[$cron['cron_id']]['last_run'] . "', next_run='" . $crons[$cron['cron_id']]['next_run'] . "', month='" . $this->db->escape($crons[$cron['cron_id']]['month']) . "', weekday='" . $this->db->escape($crons[$cron['cron_id']]['weekday']) . "', day='" . $this->db->escape($crons[$cron['cron_id']]['day']) . "', hour='" . $this->db->escape($crons[$cron['cron_id']]['hour']) . "', minute='" . $this->db->escape($crons[$cron['cron_id']]['minute']) . "', logging='" . (int)$cron['logging'] . "'");
            }else{
                $this->db->query("INSERT INTO " . DB_PREFIX . "community_crons SET file_name='" . $this->db->escape(trim($cron['file_name'])) . "', available='" . (int)$cron['available'] . "', name='" . $this->db->escape(trim($cron['name'])) . "'");
            }
        }
    }

    public function updateCron($postData, $getData, $next_run){
        if($postData['cron']['day']!='*'){
            $postData['cron']['weekday']='*';
        }

        $temp=array();

        foreach($postData['cron'] as $key => $value){
            $temp[$key]=trim($value);
        }

        $postData['cron']=$temp;

        $sql="UPDATE " . DB_PREFIX . "community_crons SET next_run='" . $next_run . "'";

        if($next_run <= time()){
            $sql.=", available='0'";
        }else{
            $sql.=", available='1'";
        }

        $sql.=", name='" . $this->db->escape($postData['cron']['name']) . "', file_name='" . $this->db->escape($postData['cron']['file_name']) . "', month='" . $this->db->escape($postData['cron']['month']) . "', weekday='" . $this->db->escape($postData['cron']['weekday']) . "', day='" . $this->db->escape($postData['cron']['day']) . "', hour='" . $this->db->escape($postData['cron']['hour']) . "', minute='" . $this->db->escape($postData['cron']['minute']) . "'";

        $sql.=" WHERE cron_id='" . (int)$getData['cron_id'] . "'";

        $this->db->query($sql);
    }

    public function  getCrons(){
        $query=$this->db->query("SELECT * FROM " . DB_PREFIX . "community_crons ORDER BY file_name ASC");

        return $query->rows;
    }

    public function getCron($data){
        $query=$this->db->query("SELECT * FROM " . DB_PREFIX . "community_crons WHERE cron_id='" . (int)$data['cron_id'] . "' LIMIT 1");

        return $query->row;
    }
}
?>